#!/bin/env python
# -*- coding: utf-8 -*-

import sys
from StringIO import StringIO
from PyQt4 import QtCore,QtGui,uic
from lxml import etree


#使用颜色来标记div，可以关闭之
color_index = 0
def get_color() :
  global color_index
  color_index += 1
  return '%02X'%(0xff-color_index*16) * 3

def styles_to_str(styles) :
  str_styles = ''
  for k,v in styles.items() :
    str_styles += '%s:%s;' % (k,v)
  #print str_styles
  return str_styles

def str_to_styles(str_styles) :
  styles = {}
  styles_list = str_styles.split(';')
  for i in styles_list :
    tmp = i.split(':')
    if len(tmp) == 2 and tmp[0] != '' :
      styles[tmp[0]] = tmp[1]
  #print styles
  return styles

#转换属性，把qt控件属性放入html的style属性中，部分不能放入style的属性不转换，如QPlainTextEdit控件的plainText属性
def get_ele_styles(qt_ele , use_color = True , default_width = '100%' , default_height = '' , default_left = '' , default_top = ''):
  has_geometry      = False
  has_max_size      = False
  has_min_size      = False
  has_size_policy   = False
  sizetype = {'v':'','h':''}
  min_size = {'width':'','height':''}
  max_size = {'width':'','height':''}
  propertys = {}

  #加上边框
  #propertys['border'] = '#000 solid 1px'

  #如果需要颜色标记，则设置背景颜色
  if use_color == True :
    #propertys['background'] = '#%s' % get_color()
    pass

  #取所有属性
  tag_propertys = qt_ele.xpath('property')
  for tag_property in tag_propertys :
    #绝对坐标:geometry
    if tag_property.get('name') == 'geometry' :
      propertys['position'] = 'absolute'
      propertys['top']      = '%spx' % ''.join(tag_property.xpath('rect/y/text()'))
      propertys['left']     = '%spx' % ''.join(tag_property.xpath('rect/x/text()'))
      propertys['width']    = '%spx' % ''.join(tag_property.xpath('rect/width/text()'))
      propertys['height']   = '%spx' % ''.join(tag_property.xpath('rect/height/text()'))
      has_geometry = True
    #大小属性:sizePolicy
    elif tag_property.get('name') == 'sizePolicy' :
      sizetype['h']  = ''.join(tag_property.xpath('sizepolicy/@hsizetype'))
      sizetype['v']  = ''.join(tag_property.xpath('sizepolicy/@vsizetype'))
      has_size_policy = True
    #最小值
    elif tag_property.get('name') == 'minimumSize' :
      min_size['width']  = ''.join(tag_property.xpath('size/width/text()'))
      min_size['height'] = ''.join(tag_property.xpath('size/height/text()'))
      has_min_size = True
    #最大值
    elif tag_property.get('name') == 'maximumSize' :
      max_size['width']  = ''.join(tag_property.xpath('size/width/text()'))
      max_size['height'] = ''.join(tag_property.xpath('size/height/text()'))
      has_max_size = True
    #边框属性
    elif tag_property.get('name') == 'frameShape' :
      propertys['border'] = '#0FF solid 2px'
    #其它属性，忽略
    else:
      pass
  ########################################################################
  #如果没有has_geometry属性（说明在layout中，或者默认位置），则查看min_size['width']和min_size['height']值
  if not has_geometry :
    if has_min_size :
      #如果 min_size['width'] 有效，则设置 width 值
      if min_size['width'] not in ['','0','16777215']  : propertys['width']  = '%spx'  % min_size['width']
      #如果 min_size['height'] 有效，则设置 height 值
      if min_size['height'] not in ['','0','16777215'] : propertys['height'] = '%spx'  % min_size['height']
    elif has_max_size :
      #如果 max_size['width'] 有效，则设置 width 值
      if max_size['width'] not in ['','0','16777215']  : propertys['width']  = '%spx'  % max_size['width']
      #如果 max_size['height'] 有效，则设置 height 值
      if max_size['height'] not in ['','0','16777215'] : propertys['height'] = '%spx'  % max_size['height']

  #如果没有width,height，则设为default值
  if default_width  != '' and not propertys.has_key('width')  : propertys['width']  = default_width
  if default_height != '' and not propertys.has_key('height') : propertys['height'] = default_height
  if default_left   != '' and not propertys.has_key('left')   : propertys['left']   = default_left
  if default_top    != '' and not propertys.has_key('top')    : propertys['top']    = default_top
  return propertys
